package cc.idoone.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

/**
 * 参考：http://www.cnblogs.com/liukemng/p/3726897.html
 */
@Controller
@RequestMapping(value = "/helloworld")
public class HelloWorldController {

    /**
     * 对一个action配置多个URL映射; 限制action所接受的请求方式
     */
    @RequestMapping(value = {"/index", "/hello"}, method = {RequestMethod.GET, RequestMethod.POST})
    public ModelAndView index() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("message", "Hello World!");
        modelAndView.setViewName("index");
        return modelAndView;
    }

    /**
     * URL请求参数映射
     */
    @RequestMapping(value = "/detail/{id}", method = {RequestMethod.GET})
    public ModelAndView getDetail(@PathVariable(value = "id") Integer id) {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("id", id);
        modelAndView.setViewName("detail");
        return modelAndView;
    }

    /**
     * URL通配符映射
     */
    @RequestMapping(value = "/*", method = {RequestMethod.GET})
    public ModelAndView urlTest() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("urltest");
        return modelAndView;
    }

    /**
     * 通配符有“？”和“*”两个字符。其中“？”表示1个字符，“*”表示匹配多个字符，“**”表示匹配0个或多个路径
     */
    @RequestMapping(value = "/**", method = {RequestMethod.GET})
    public ModelAndView urlAllTest() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("message", "Hello /**");
        modelAndView.setViewName("index");
        return modelAndView;
    }

    @RequestMapping(value = {"/index?", "/hello"}, method = {RequestMethod.GET})
    public ModelAndView indexOne() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("message", "Hello /index?");
        modelAndView.setViewName("index");
        return modelAndView;
    }

    @RequestMapping(value = {"/index*", "/hello"}, method = {RequestMethod.GET})
    public ModelAndView indexMore() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("message", "Hello /index*");
        modelAndView.setViewName("index");
        return modelAndView;
    }

    @RequestMapping(value = {"/index/*", "/hello"}, method = {RequestMethod.GET})
    public ModelAndView indexPath() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("message", "Hello /index/*");
        modelAndView.setViewName("index");
        return modelAndView;
    }

    @RequestMapping(value = {"/index/**", "/hello"}, method = {RequestMethod.GET})
    public ModelAndView indexPathMore() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("message", "Hello /index/**");
        modelAndView.setViewName("index");
        return modelAndView;
    }

    /**
     * URL正则表达式映射
     */
    @RequestMapping(value = "/reg/{name:\\w+}-{age:\\d+}", method = {RequestMethod.GET})
    public ModelAndView regUrlTest(@PathVariable(value = "name") String name, @PathVariable(value = "age") Integer age) {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("name", name);
        modelAndView.addObject("age", age);
        modelAndView.setViewName("regurltest");
        return modelAndView;
    }

    /**
     * 限制action所接受请求的参数: 指定映射请求必须包含某参数<br/>
     * 注：当我们为params指定多个参数时如：params={"example1", "example2"}，表示的是and关系，即两个参数限制必须同时满足
     */
    @RequestMapping(value = "/params/exists", params = {"example"}, method = {RequestMethod.GET})
    public ModelAndView paramsExists() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("message", "Hello /params/exists");
        modelAndView.setViewName("index");
        return modelAndView;
    }

    /**
     * 限制action所接受请求的参数: 指定映射请求必须不包含某参数
     */
    @RequestMapping(value = "/params/notExists", params = "!example", method = {RequestMethod.GET})
    public ModelAndView paramsNotExists() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("message", "Hello /params/notExists");
        modelAndView.setViewName("index");
        return modelAndView;
    }

    /**
     * 限制action所接受请求的参数: 指定映射请求中或者某参数必须等于某个值
     */
    @RequestMapping(value = "/params/equals", params = "example=AAA", method = {RequestMethod.GET})
    public ModelAndView paramsEquals() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("message", "Hello /params/equals");
        modelAndView.setViewName("index");
        return modelAndView;
    }

    /**
     * 限制action所接受请求的参数: 指定映射请求中或者某参数必须不等于某个值
     */
    @RequestMapping(value = "/params/notEquals", params = "example!=AAA", method = {RequestMethod.GET})
    public ModelAndView paramsNotEquals() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("message", "Hello /params/notEquals");
        modelAndView.setViewName("index");
        return modelAndView;
    }

    /**
     * 限制action所接受请求头参数: 指定映射请求头必须包含某参数<br/>
     * 注：当我们为headers指定多个参数时如：headers={"example1", "example2"}，表示的是and关系，即两个参数限制必须同时满足
     */
    @RequestMapping(value = "/headers/exists", headers = {"Connection"}, method = {RequestMethod.GET})
    public ModelAndView headerExists() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("message", "Hello /header/exists");
        modelAndView.setViewName("index");
        return modelAndView;
    }

    /**
     * 限制action所接受请求头参数: 指定映射请求头必须不包含某参数
     */
    @RequestMapping(value = "/headers/notExists", headers = "!Connection", method = {RequestMethod.GET})
    public ModelAndView headerNotExists() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("message", "Hello /header/notExists");
        modelAndView.setViewName("index");
        return modelAndView;
    }

    /**
     * 限制action所接受请求头参数: 指定映射请求头中或者某参数必须等于某个值
     */
    @RequestMapping(value = "/headers/equals", headers = "Connection=keep-alive", method = {RequestMethod.GET})
    public ModelAndView headerEquals() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("message", "Hello /header/equals");
        modelAndView.setViewName("index");
        return modelAndView;
    }

    /**
     * 限制action所接受请求头参数: 指定映射请求头中或者某参数必须不等于某个值
     */
    @RequestMapping(value = "/headers/notEquals", headers = "Connection!=keep-alive", method = {RequestMethod.GET})
    public ModelAndView headerNotEquals() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("message", "Hello /header/notEquals");
        modelAndView.setViewName("index");
        return modelAndView;
    }

}
